package csc.fresher.controller;

import java.io.IOException;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import csc.fresher.bankservice.BankService;
import csc.fresher.domain.json.UserTemp;
import csc.fresher.domain.Role;
import csc.fresher.domain.Status;
import csc.fresher.domain.UserInfo;

@Controller
@RequestMapping(value = "/updateinfo")
public class UpdateInfoController {

	@Autowired
	private BankService bankService;
	
	@ModelAttribute("usertemp")
	private UserTemp contructUserTemp() {
		return new UserTemp();
	}

	@RequestMapping
	public String goUpdateInfo(Model model) {
		List<UserInfo> list = bankService.userNomal();
		model.addAttribute("users", list);
		HomeController.page = "update";
		return "updateinfo";
	}

	@RequestMapping(value = "/edit/{loginId}", method = RequestMethod.GET)
	@ResponseBody
	public UserTemp modalEdit(@PathVariable("loginId") String loginId) {
		UserInfo user = bankService.getUserInfo(loginId);

		UserTemp userTemp = new UserTemp(user.getId() ,user.getLoginId(), user.getStatus()
				.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
				user.getCardnumberid(), user.getFirstname(),
				user.getLastname(), user.getMidname(), user.getPhone1(),
				user.getPhone2(), user.getAddress1(), user.getAddress2(),
				user.getEmail1(), user.getEmail2(), user.getImage());

		return userTemp;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateUser(
			final RedirectAttributes attrs,
			@Valid @ModelAttribute("usertemp") UserTemp userTemp,
			BindingResult rs,
			@RequestParam(value = "fileUpload", required = false) MultipartFile file,
			@RequestParam(value = "passwordNewAgain", required = false) String passwordNew) {

		if (rs.hasErrors()) {
			attrs.addFlashAttribute("message", "Something wrong");
			return "redirect:/updateinfo";
		}
		
		if(!passwordNew.equals("")){
			userTemp.setPassword(bankService.hashPasswordSHA(passwordNew));
		}
		
		if (!file.isEmpty()) {
			byte[] image = null;
			try {
				image = file.getBytes();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			userTemp.setImage(image);
		}
		
		if(file.getOriginalFilename().equals("")){
			UserInfo user = bankService.getUserInfo(userTemp.getLoginId());
			userTemp.setImage(user.getImage());
		}
		
		Role role = bankService.getRole(userTemp.getRoleId());
		Status status = bankService.getStatus(userTemp.getStatusId());
		
		UserInfo userinfo = new UserInfo(userTemp.getId(), userTemp.getLoginId(), status , role, userTemp.getPassword(),
				userTemp.getCardnumberid(), userTemp.getFirstname(),
				userTemp.getLastname(), userTemp.getMidname(), userTemp.getPhone1(),
				userTemp.getPhone2(), userTemp.getAddress1(), userTemp.getAddress2(),
				userTemp.getEmail1(), userTemp.getEmail2(), userTemp.getImage());
		
		bankService.update(userinfo);
		
		attrs.addFlashAttribute("message", "Update Successfully");
		return "redirect:/updateinfo";
	}
	
}
